﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Supervise_TraGoodsHospitalPlan : System.Web.UI.Page
{
    public string _strSupplyGoodsId = "", _strEndDate, _strStartDate, _AreaId = "", _strUserType = "", _strAccountId = "";
    string strSor = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        string strSql,_strCompanyName_PS;
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumHospitale);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumSupervise);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCompany);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumSuperviseChildCheck);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["deal"] != null && Request.QueryString["deal"] != "")
                {
                    switch (Request.QueryString["deal"].ToString())
                    {
                        case "未确认送货":
                            dealDrop.SelectedIndex = 0;
                            break;
                        case "送货中":
                            dealDrop.SelectedIndex = 1;
                            break;
                        case "已入库":
                            dealDrop.SelectedIndex = 2;
                            break;
                        case "无法配送":
                            dealDrop.SelectedIndex = 3;
                            break;
                        case "到期未送":
                            dealDrop.SelectedIndex = 4;
                            break;

                    }

                }
            }
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (_Account != null)
            {
                _strUserType = SysFun.ToTrim(_Account.Type);
                _strAccountId = SysFun.ToTrim(_Account.AccountId);
               strSor = SysFun.ToTrim(_Account.Sort);
               if (strSor == "0")
               {
                   _AreaId = "530000";
               }
               else
               {
                   _AreaId = SysFun.ToTrim(_Account.AreaId);
               }
               
            }
            _strSupplyGoodsId = SysFun.IsSQL(Request.QueryString["SupplyGoodsId"]);

            if (!IsPostBack)
            {
                //<!--初始化统计时间
                int Year, Month, Day, DMonth, Year1;
                Year = DateTime.Now.Year;
                Year1 = Year;
                DMonth = DateTime.Now.Month;
                Month = DMonth - 1;
                if (Month == 0)
                {
                    Year1 -= 1;
                    Month = 12;
                }

                Day = DateTime.Now.Day;
                txtStartDate.Text = Year1.ToString() + "-" + Month.ToString() + "-01";
                txtEndDate.Text = Year.ToString() + "-" + DMonth.ToString() + "-01";

                _strEndDate = SysFun.IsSQL(Request.QueryString["EndDate"]);
                _strStartDate = SysFun.IsSQL(Request.QueryString["StartDate"]);
                if (_strEndDate != "")
                {
                    txtEndDate.Text = _strEndDate;
                }
                if (_strStartDate != "")
                {
                    txtStartDate.Text = _strStartDate;
                }
                //初始化统计时间-->
                _strCompanyName_PS = SysFun.IsSQL(Request.QueryString["CompanyName_PS"]);
                if (_strCompanyName_PS != "")
                {
                    labCompanyName_PS.Text = _strCompanyName_PS;
                    labCompanyName_PS.Visible = true;
                    Label1.Visible = true;
                }
                else
                {
                    labCompanyName_PS.Visible = false;
                    Label1.Visible = false;
                }
                //<!--初始化配送企业

                //初始化配送企业-->                
                Db DbProject = new Db();
                System.Data.IDataReader dr;
                //<!--初始化商品
                if (_strSupplyGoodsId != "")
                {
                    strSql = "select GoodsId,productname,GoodsName,medicinemodel,outlookc,factor,companyName_SC,CASE QualityLevels " + ConfigurationManager.AppSettings["QualityLevels"].ToString() + " END AS  QualityLevels,convert(numeric(20,2),BidPrice) as BidPrice from SupplyPlan_Goods where SupplyGoodsId=" + _strSupplyGoodsId;
                    dr = DbProject.DataReader(strSql);
                    while (dr.Read())
                    {
                        labGoodsId.Text = dr["GoodsId"].ToString();
                        labProductName.Text = dr["productname"].ToString();
                        labGoodsName.Text = dr["GoodsName"].ToString();
                        labMedicinemodel.Text = dr["medicinemodel"].ToString();
                        labOutlookc.Text = dr["outlookc"].ToString();
                        labFactor.Text = dr["factor"].ToString();
                        labCompanyName.Text = dr["companyName_SC"].ToString();
                        labQualityLevels.Text = dr["QualityLevels"].ToString();
                        labBidPrice.Text = dr["BidPrice"].ToString();
                    }
                    dr.Close();
                    dr.Dispose();
                }
                //初始化商品-->

                DbProject.Close();
            }

            if (_strStartDate != "" && _strEndDate != "")
            {
                if (Request.Form["butSelectAll"] == "查看全部")
                {

                    strSql = @" select AccountId_YY,HospitalName,UserCode_YY,BuyPlanId,PlanName,StartTime,EndTime,convert(numeric(20,2),tradeprice) as tradeprice,PurchasePlanDate,sum(inflownum) as RKSL ,
                            convert(numeric(20,2),sum(GoodsNum*tradeprice)) as DGJE , convert(numeric(20,2),sum(inflownum*tradeprice)) as DHJE 
                            from tra_GoodsId where  {0}  group by AccountId_YY,HospitalName,UserCode_YY,BuyPlanId,PlanName,StartTime,EndTime,convert(numeric(20,2),tradeprice),PurchasePlanDate order by DHJE DESC,BuyPlanId";
                    string _strtempWhere = fctConditions();
                    strSql = String.Format(strSql, _strtempWhere);
                    Db db1 = new Db();
                    System.Data.IDataReader dr;
                    dr = db1.DataReader(strSql);
                    GridView1.DataSource = dr;
                    GridView1.DataBind();
                    dr.Close();
                    dr.Dispose();
                    db1.Close();
                }
                else
                {
                    showGridView();
                }
            }
        }
    }
    protected void Button5_Click(object sender, EventArgs e)
    {

    }

    /// <summary>
    /// 数据绑定
    /// </summary>
    public void showGridView()
    {

        string strConditions,
            _SelectField = @" AccountId_YY,HospitalName,UserCode_YY,BuyPlanId,PlanName,StartTime,EndTime,convert(numeric(20,2),tradeprice) as tradeprice,PurchasePlanDate,sum(inflownum) as RKSL ,
                            convert(numeric(20,2),sum(GoodsNum*tradeprice)) as DGJE , convert(numeric(20,2),sum(inflownum*tradeprice)) as DHJE,case DealStatus  when 0 then '未确认送货' when 1 then '送货中' when 2 then '已入库' when 4 then '无法配送' when 5 then '到到期未送' end as DealStatus,convert(varchar(200),hospitalinflowdate,23) as hospitalinflowdate ";
        strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "tra_GoodsId";                                               //要查询的表明
        dbPage.SelectFieldGroup = "AccountId_YY,HospitalName,UserCode_YY,BuyPlanId,PlanName,StartTime,EndTime,convert(numeric(20,2),tradeprice),PurchasePlanDate,DealStatus,hospitalinflowdate";
        dbPage.Key = "BuyPlanId";                                               //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = _SelectField;//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 30;                                                    //没有显示记录条数   
        dbPage.OrderBy = "DHJE,BuyPlanId";                  //排序字段
        dbPage.OrderByIsDesc = true;
        dbPage.DGJE = "convert(numeric(20,2),sum(GoodsNum*TradePrice)) as DGJE";
        dbPage.DHJE = "convert(numeric(20,2),sum(inflownum*TradePrice)) as DHJE";
        System.Data.IDataReader dr = dbPage.ExecuteReaderGroup(60);
        GridView1.DataSource = dr;
        GridView1.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键

    }
    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {
        string _strTemp = " DealStatus >=0 and DealStatus<>3 and", _strBuyPlanId, _strPlanName, _strAccountId_PS;

        if (dealDrop.SelectedValue != "")
        {
            if (dealDrop.SelectedValue == "0")
            {
                _strTemp += " DealStatus=" + dealDrop.SelectedValue + " and buystatus=2 and";
            }
            else
            {
                _strTemp += " DealStatus=" + dealDrop.SelectedValue + " and";
            }
        }
        //挂网目录品目号
        if (_strSupplyGoodsId != "")
        {
            _strTemp += "  SupplyGoodsId=" + _strSupplyGoodsId + " and";
        }
        //时间范围
        _strEndDate = txtEndDate.Text.ToString();
        _strStartDate = txtStartDate.Text.ToString();
        if (_strEndDate != "" && _strStartDate != "")
        {
            _strTemp += " isnull(hospitalinflowdate,'3000-12-30') >='" + _strStartDate + "' and isnull(hospitalinflowdate,'1900-01-01') <='" + _strEndDate + "' and";
        }

        //如果当前使用人是配送企业
        if (_strUserType == "2")
        {
            if (strSor == "0")
            {
                _strTemp += " AccountId_SC=" + _strAccountId + " and";
                //配送企业
                _strAccountId_PS = SysFun.ToTrim(Request.QueryString["AccountId_PS"]);
                if (_strAccountId_PS != "")
                {
                    _strTemp += "  AccountId_PS=" + _strAccountId_PS + " and";
                }
            }
            else
            {
                _strTemp += " AccountId_PS=" + _strAccountId + " and";
            }
        }
        else
        {
            //配送企业
            _strAccountId_PS = SysFun.ToTrim(Request.QueryString["AccountId_PS"]);
            if (_strAccountId_PS != "")
            {
                _strTemp += "  AccountId_PS=" + _strAccountId_PS + " and";
            }

        }

        //医院id
        if (_strUserType == "3")
        {
            _strTemp += "  AccountId_YY=" + _strAccountId + " and";
        }
        else
        {
            _strAccountId = SysFun.ToTrim(Request.QueryString["AccountId_YY"]);
            if (_strAccountId != "")
            {
                _strTemp += "  AccountId_YY=" + _strAccountId + " and";
            }
            _strTemp += SysFun.AreaIdWhere(_AreaId);
        }

        //采购单号
        _strBuyPlanId = txtBuyPlanId.Text.ToString();
        if (_strBuyPlanId != "")
        {
            _strTemp += " BuyPlanId like '%" + _strBuyPlanId + "%' and";
        }

        //采购单名称
        _strPlanName = txtPlanName.Text.ToString();
        if (_strPlanName != "")
        {
            _strTemp += "  PlanName like '%" + _strPlanName + "%' and";
        }

        if (_strTemp != "")
        {
            _strTemp = "  " + _strTemp.Substring(0, _strTemp.Length - 3);
        }
        return _strTemp;
    }
}
